package com.zjitc.common;

import com.zjitc.utils.old.MyNumber;

/**
 * Create by IntelliJ IDEA
 *
 * @author: jsonor
 * @date-Time: 2018/3/27 15:59
 * @description:
 */
public class ServerResponse<T> {

  private Integer status;
  private T data;
  private String msc;

  public ServerResponse() {
  }

  private ServerResponse(String msc) {
    this.msc = msc;
  }

  private ServerResponse(Integer status, T data, String msc) {
    this.status = status;
    this.data = data;
    this.msc = msc;
  }

  private ServerResponse(Integer status, String msc) {
    this.status = status;
    this.msc = msc;
  }

  private ServerResponse(Integer status, T data) {
    this.status = status;
    this.data = data;
  }

  private ServerResponse(Integer status) {
    this.status = status;
  }

  public Integer getStatus() {
    return status;
  }

  public void setStatus(Integer status) {
    this.status = status;
  }

  public T getData() {
    return data;
  }

  public void setData(T data) {
    this.data = data;
  }

  public String getMsc() {
    return msc;
  }

  public void setMsc(String msc) {
    this.msc = msc;
  }

  public boolean success() {
    return this.status == 0;
  }

  public static ServerResponse returnmsc(String arg) {
    return new ServerResponse(arg);
  }

  public static <T> ServerResponse<T> createSuccess(Object... args) {
    return create(0, args);
  }

  public static <T> ServerResponse<T> createError(Object... args) {
    return create(1, args);
  }

  public static <T> ServerResponse<T> createBackError(Object... args) {
    return create(10, args);
  }

  public static <T> ServerResponse<T> createinfomationError(Object... args) {
    return create(10, args);
  }

  private static <T> ServerResponse<T> create(Integer status, Object... args) {
    if (args.length > MyNumber.TWO.ordinal()) {
      throw new IllegalArgumentException("传入参数超过 2个");
    }

    if (args.length == MyNumber.ONE.ordinal()) {
      if (args[0] instanceof String) {
        return new ServerResponse(status, (String) args[0]);
      } else {
        return new ServerResponse<T>(status, (T) args[0]);
      }
    }

    if (args.length == MyNumber.TWO.ordinal()) {
      return new ServerResponse<T>(status, (T) args[0], (String) args[1]);
    }

    return new ServerResponse(status);
  }
}
